declare @Cliente varchar(14)

set nocount on
declare TmpCliente cursor for select codigo_cliente from clientes_varejo
open TmpCliente
fetch next from TmpCliente into @Cliente

While @@fetch_status = 0
begin
	update clientes_varejo set ultima_compra = (select isnull(max(data_venda),'19000101') from loja_venda where codigo_cliente = @Cliente) where codigo_cliente = @Cliente
	fetch next from TmpCliente into @Cliente
end

close TmpCliente
deallocate TmpCliente

set nocount off
